const express=require('express')
const multer=require('multer')
const pool=require('../pool')
const app=express()
app.listen(6004,()=>{
    console.log('苏富比头像上传服务已启动，端口6004，请勿关闭此窗口')
})
const cors=require('cors')
app.use(cors({
    origin:['http://localhost:6001/','http://localhost:8080','http://localhost:6003']
}))
let storage=multer.diskStorage({
    destination(req,file,cb){
        cb(null,'avatar/img/')
    },
    filename(req,file,cb){
        let name=file.originalname
        let ext=name.substr(name.lastIndexOf('.'))
        cb(null,uuid.v4()+ext)
    }
})
const uuid=require('uuid')
app.use(express.static('avatar')) 
const uploadTools = multer({storage:storage})
// 文件上传
app.post('/sufubi/avatar',uploadTools.single('avatar'),(req,res)=>{
    const {sid} = req.body
    const path=`http://localhost:6004/img/${req.file.filename}`
    pool.query('UPDATE su_user SET avatar=? WHERE sid=?',[path,parseInt(sid)],(err,result)=>{
        if(err){
            res.send({status:404,details:'Avatar Upload Server Error...'})
            return
        }
        res.send({status:'OK',code:200,details:'Avatar update success!',path})
    })
})